package org.example.domain.strategy.service.rule.tree.impl;


import lombok.extern.slf4j.Slf4j;
import org.example.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import org.example.domain.strategy.service.rule.tree.ILogicTreeNode;
import org.example.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import org.example.types.common.Constants;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component("rule_luck_award")
public class RuleLuckAwardLogicTreeNode implements ILogicTreeNode {
    @Override
    public DefaultTreeFactory.TreeActionEntity logic(String userId, Long strategyId, Integer awardId, String ruleValue, Date endDateTime) {
        log.info("规则过滤-兜底奖品 userId:{} strategyId:{} awardId:{} ruleValue:{}",userId,strategyId,awardId,ruleValue);
        String[] split = ruleValue.split(Constants.COLON);
        if(split.length == 0){
            log.error("规则过滤-兜底奖品,兜底奖品未配置警告 userId:{} strategyId:{} awardId:{} ",userId,strategyId,awardId);
            throw new RuntimeException("兜底奖品未配置" + ruleValue);
        }

        Integer luckAwardId = Integer.valueOf(split[0]);
        String awardRuleValue = split.length > 1 ? split[1] : "";

        //返回兜底奖品
        log.info("规则过滤-兜底奖品 userId:{} strategyId:{} luckAwardId:{} awardRuleValue:{}",userId,strategyId,luckAwardId,awardRuleValue);
        return DefaultTreeFactory.TreeActionEntity.builder()
                .ruleLogicCheckType(RuleLogicCheckTypeVO.TAKE_OVER)
                .strategyAwardData(DefaultTreeFactory.StrategyAwardVO.builder()
                        .awardId(luckAwardId)
                        .awardRuleValue(awardRuleValue)
                        .build())
                .build();
    }
}
